package com.xq.controller;


import java.io.IOException;

import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

//Serialize Number to string and Deserialize string to Double (note, "" to null)
public class GsonNumberToStringAdaptor extends TypeAdapter<Number>
{

	@Override
	public void write(JsonWriter out, Number value) throws IOException
	{
		out.value(value.toString());
	}

	@Override
	public Number read(JsonReader in) throws IOException
	{
		if (in.peek() == JsonToken.NULL)
		{
			in.nextNull();
			return null;
		}
		try
		{
			String result = in.nextString();
			if ("".equals(result))
			{
				return null;
			}
			return Double.parseDouble(result);
		}
		catch (NumberFormatException e)
		{
			throw new JsonSyntaxException(e);
		}
	}

}
